import bean.Detail;
import bean.GoodInfo;
import bean.Member;
import bean.Product;
import dao.*;
import impl.*;
import org.junit.jupiter.api.Test;
import service.MemberService;
import service.impl.MemberServiceImpl;
import util.InputUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname:index
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/31 16:26
 * @version: 0.1
 * @since:1.8
 */
public class index {

    @Test
    public void selectMemberAll() {
        MemberService memberService = new MemberServiceImpl();
//    memberService.selectAll();
    }

    @Test
    public void updatesBalance() {
        MemberDao memberDao = new MemberDaoImpl();
        int i = memberDao.updateMemberBalance(2, 100);
        System.out.println(i);
    }

    @Test
    public void selectMemberOneById() {
        MemberDao memberDao = new MemberDaoImpl();
        Member member = memberDao.selectMemberById(5);
        System.out.println(member);
    }

    @Test
    public void selectAllProduct() {
        ProductDao productDao = new ProductDaoImpl();
        productDao.selectAll().forEach((k) -> {
            if (k.getIs_parent() != 1) {
                System.out.println("||||" + k);
            } else {
                System.out.println(k);
            }
        });
    }

    @Test
    public void seletctGoodByPrarms() {

        Map<String, Object> map = new LinkedHashMap<>(16);
        map.put("gname", "方便面");
        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        goodInfoDao.selectGoodByParams(map).forEach(System.out::println);
    }

    @Test
    public void updateGoodInfo() {
        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        GoodInfo goodInfo = goodInfoDao.selectGoodByPid(6);
        goodInfo.setGname("banan");
        goodInfo.setDiscount(9f);
        int i = goodInfoDao.updateGoodInfoById(goodInfo, goodInfo.getId());
        System.out.println(i);
    }

    @Test
    public void selectGoodById() {
        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        GoodInfo goodInfo = goodInfoDao.selectGoodById(4);
        System.out.println(goodInfo);
    }

    @Test
    public void selectProductById() {
        ProductDao productDao = new ProductDaoImpl();
        Product product = productDao.selectProductById(2);
        System.out.println(product);
    }

    @Test
    public void addDetail() {
        DetailDao detailDao = new DetailDaoImpl();
        Detail detail = new Detail();
        detail.setUid(1);
        detail.setId(1);
        detail.setCount(10);

//        System.out.println(detailDao.addDetail(detail));
    }

    @Test
    public void addOrderAndDetial(){
        OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
        Map<Integer,Integer> map  = new LinkedHashMap<>(16);
        map.put(1,12);
        map.put(2,23);
        map.put(3,12);
        orderInfoDao.addOrderAndDetail(3,map);
    }

    @Test
    public void updateOrderInfoNumber(){
        OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
        int i = orderInfoDao.updateGoodNumberById(10, 1);
        System.out.println(
                i
        );
    }

    @Test
    public void rand(){
        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        goodInfoDao.rankByType().forEach(System.out::println);
    }

    @Test
    public static void productTree(){
        System.out.println("请输入父级Id");
        int pid  = InputUtil.inputInt();
        showInfoByTree("|-",pid);

    }
    private static void showInfoByTree(String s,int pid){
        ProductDao productDao = new ProductDaoImpl();
        Map<String,Object> map = new HashMap<>(16);
        map.put("is_parent",pid);
        List<Product> list = productDao.selectProductByParams(map);
        for (Product product : list) {
            System.out.println(s+product.getTypeName());
            if(product.getIs_parent()==0){
                showInfoByTree("|"+s,product.getParentId());
            }
        }

    }


    public static void main(String[] args) {
        productTree();
    }

}
